Ext.namespace("Nippy.Components.Grids");

function ClipboardImageItem(id,sizeId){
	this.Id = id;
	this.SizeId = sizeId;
}

Nippy.Components.Grids.ImageSizes = Ext.extend(Ext.grid.EditorGridPanel, {
	constructor: function(config){
		Nippy.Components.Grids.ImageSizes.superclass.constructor.apply(this, arguments);
    }
	,initComponent: function(){
		Ext.apply(this, {
    		cm: /*START ColumnModel*/
    			new Ext.grid.ColumnModel(
				    [{
    				        header: 'Размер',
    				        dataIndex: 'SizeName',
    				        width: 100,
    				        readonly: true
				      },{
    				        header: 'Alt',
    				        dataIndex: 'Alt',
    				        width: 180,
    				        editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})
				      },{
					        header: 'Title',
					        dataIndex: 'Title',
					        width: 180,
					        editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})
					  },{
    				        header: 'Ширина',
    				        dataIndex: 'Width',
    				        width: 50
    				        //editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})
					  },{
						  	header: 'Высота',
      				        dataIndex: 'Height',
      				        width: 50
      				        //editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})
				      }]
				    ),
    			/*END ColumnModel*/
    		store: new Nippy.Components.Grids.ImageStore(),
    		clicksToEdit:2,
    		selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
			tbar: [ {text: 'Заменить', scope: this, handler: this.replaceClick}
			,{text: 'Редактировать', scope: this, handler: this.editClick}
			,{text: 'Копировать', scope: this, handler: this.copyClick}
			,{text: 'Вставить', scope: this, handler: this.pasteClick}
			]
    	});
		
		Nippy.Components.Grids.ImageSizes.superclass.initComponent.apply(this, arguments);
	}
	,load: function(imageId){
		this.imageId = imageId;
		this.getStore().load({params: {id: this.imageId , sizeName: 1}});
	}
	,replaceClick: function(){
		var r = this.getSelectionModel().getSelected();
		if (undefined == r) return;
		var wnd = new Nippy.Components.Dialogs.UploadImage({
			replace: {Id: r.data.Id,SizeId: r.data.SizeId},
			closable: true,width: 400,modal: true,resizable: false});
		wnd.addListener('onuploaded',this.imageUploaded,this);
		wnd.show(r.data.Alt,r.data.Title);
	}
	,imageUploaded: function(){
		var r = this.getSelectionModel().getSelected();
		this.getStore().load({params: {id: this.imageId, sizeName: 1}});
	}
	,editClick: function(){
		var r = this.getSelectionModel().getSelected();
		if (undefined == r) return;
		var u=location.protocol+'//'+location.host
		var p=u+'/pics/gallery/'+r.data.Id+'_'+r.data.SizeId+'_'+r.data.Width+'x'+r.data.Height+'.'+r.data.Ext;
		Ext.Ajax.request({
			url: '/api/pixlr/get-token/'
			,success: function(rsp){
				var t = eval("("+rsp.responseText+")");
				var w = window.open('http://pixlr.com/editor/?image='+p+'&title='+r.data.Title+'&target='+u+'/api/pixlr/index/'+r.data.Id+'/'+r.data.SizeId+'/'+t.token+'/&exit='+u,'_blank');
			}
			,failure: function(){
				alert("Произошла ошибка");
			}
			,params: { Id: r.data.Id, SizeId: r.data.SizeId}
		});
	}
	,copyClick: function(){
		var r = this.getSelectionModel().getSelected();
		if (undefined == r) return;
		window.clipboard = new ClipboardImageItem(r.data.Id,r.data.SizeId);
	}
	,pasteClick: function(){
		var r = this.getSelectionModel().getSelected();
		if (undefined == r) return;
		var b=window.clipboard;
		if (!(b instanceof ClipboardImageItem)) {alert('В буфере обмена находится объект другого класса');}
		Ext.Ajax.request({
			url: '/admin/gallery-item/index/?subaction=paste'
			,method: "GET"
			,scope: this
			,success: function(rsp){
				this.getStore().reload({params: {id: this.imageId , sizeName: 1}});
				//var t = eval("("+rsp.responseText+")");
			}
			,failure: function(){
				alert("Произошла ошибка");
			}
			,params: { SrcId: b.Id, SrcSizeId: b.SizeId, DstId: r.data.Id, DstSizeId: r.data.SizeId}
		});
	}
});

Nippy.Components.Grids.ImageStore = Ext.extend(Ext.data.Store, {
	constructor: function(config){
		Ext.apply(this,{
			restful: true,
			proxy: new Ext.data.HttpProxy({
				url: '/admin/gallery-item',
				restful: true		
			}),
		    
		    reader: new Ext.data.JsonReader({
			    	root: 'data',
					totalProperty: '',
					idProperty: 'CompositeId'
				},[
				   	{name: 'CompositeId', type: 'string', mapping: 'CompositeId'},
				    {name: 'Id', type: 'int', mapping: 'Id'},
			    	{name: 'SizeId', type: 'int', mapping: 'SizeId'},
					{name: 'SizeName', type: 'string', mapping: 'SizeName'},
					{name: 'Alt', type: 'string', mapping: 'Alt'},
					{name: 'Title', type: 'string', mapping: 'Title'},
					{name: 'Width', type: 'int', mapping: 'Width'},
					{name: 'Height', type: 'int', mapping: 'Height'},
					{name: 'Ext', type: 'string', mapping: 'Ext'}
			]),
			writer:new Ext.data.JsonWriter({encode: false})
		});
		Nippy.Components.Grids.ImageStore.superclass.constructor.apply(this, arguments);
	}
});